home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c,gnu.gcc.help
- Subject: Re: Casting unsigned short as unsigned int -> Bus error
- Date: Sat, 27 Jan 96 00:11:57 GMT
- Organization: none
- Message-ID: <822701517snz@genesis.demon.co.uk>
- References: <simmons.820857453@rzdspc1> <820879186snz@genesis.demon.co.uk> <4e8v6t$bd2@crl.crl.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4e8v6t$bd2@crl.crl.com> gherlein@crl.com "Greg Herlein" writes:
-
- >This sounds to me like a problem I had a while back - though I'm not a
- >Sparc guru enough to know for sure. I was reading elements from
- >structures like this too, and got bus errors for not reading on 32 bit
- >boundaries. Does it work if you read the myshort variable into a
- >short *then* cast it to an unsigned int? I'm not sure what the order
- >of operations is on the assignment... I would think the dererencing of
- >the stucture element poinet would be first, but what do I know?
-
- Of course there is always the possibility of a compiler bug (but it is
- unlikely). (Almost) the only way in C to get invalid memory accesses is
- through non-portable pointer operations (e.g. casting to a type with
- stricter alignment requirements). In the case of:
-
- >: > typedef struct {
- >: > ...
- >: > unsigned short myshort;
- >: > ...
- >: > } RecType;
- >: >
- >: > RecType *MyRec;
- >: >
- >: >Now when I try to dereference that member, casting it to (unsigned int),
- >: >I get a bus error:
- >: >
- >: > unsigned int myint;
- >: >
- >: > myint = (unsigned int) MyRec->myshort; /* Bus error! */
-
- the normal sequence of operations is to read an unsigned short from
- MyRec->myshort, convert the value to unsigned int and write it to myint.
- The only way that can fail is if MyRec isn't pointing to a valid RecType
- object (either not a valid address or not correctly aligned).
-
- >: >I'm using GCC 2.7.2 on a SparcStation running SunOS 4.1.4. I've made sure
- >: >that the pointer points to valid data.
-
- This is something you need to recheck. Print out the pointer value and see if
- it is on an even address (assuming short is 2 'bytes' long).
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-